These samples show how to integrate C# code with LINQ queries.

[Category("Language Integration")]
[Description("Find all orders for customers in a List")]
public void XLinq79()
{
XDocument doc = XDocument.Load("nw_orders.xml");
List<Customer> customers = SomeMethodToGetCustomers();
var result =
from customer in customers
from order in doc.Descendants("Orders")
where
customer.id == (string)order.Element("CustomerID")
select
new { custid = (string)customer.id, orderdate = (string)order.Element("OrderDate") };
foreach (var tuple in result)
Console.WriteLine("Customer id = {0}, Order Date = {1}",
tuple.custid, tuple.orderdate);
}
class Customer
{
public Customer(string id) { this.id = id; }
public string id;
}
static List<Customer> SomeMethodToGetCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer("VINET"));
customers.Add(new Customer("TOMSP"));
return customers;
}
[Category("Language Integration")] [Description("Find all orders for customers in a List")] public void XLinq79() { XDocument doc = XDocument.Load("nw_orders.xml"); List<Customer> customers = SomeMethodToGetCustomers(); var result = from customer in customers from order in doc.Descendants("Orders") where customer.id == (string)order.Element("CustomerID") select new { custid = (string)customer.id, orderdate = (string)order.Element("OrderDate") }; foreach (var tuple in result) Console.WriteLine("Customer id = {0}, Order Date = {1}", tuple.custid, tuple.orderdate); } class Customer { public Customer(string id) { this.id = id; } public string id; } static List<Customer> SomeMethodToGetCustomers() { List<Customer> customers = new List<Customer>(); customers.Add(new Customer("VINET")); customers.Add(new Customer("TOMSP")); return customers; }
For more information on LINQ to XML: http://msdn.microsoft.com/en-us/library/bb387098.aspx